#ifndef __NOTIFY_H__
#define __NOTIFY_H__

#include <list>
#include <map>
#include <thread>
#include <mutex>
#include <condition_variable>
#include "df.h"

class IMessage;
class IHandle;

class MGNOTIFY_API mgnotify
{
public:
    static mgnotify* getInstance();

    void attach(IHandle* handle);
    void detach(IHandle* handle);

    // 同步消息
    bool sendMessage(const IMessage* msg);
    // 异步消息
    void pushMessage(IMessage* msg, MessagePriority priority);

    // 消息类个数
    int senderCount() const;
    // 消息类名称列表
    std::list<std::string> senders();
    // 消息类对应接受者个数
    int receiverCount(std::string sender);
    
    ~mgnotify();

private:
    mgnotify();
    mgnotify(const mgnotify &other) = delete;
    mgnotify &operator=(const mgnotify &) = delete;

    void run();
private:
    typedef std::list<IMessage*>            LMsg;
    typedef std::list<IHandle*>             LHandle;
    typedef std::map<std::string, LHandle*> MHandle;

    static mgnotify*        self;
    static std::mutex       qmutex;

    LMsg                    msglist;
    LHandle                 hanlist;
    MHandle                 mhandle;

    bool                    isrunning;
    std::thread             mthread;
    std::mutex              tmutex;
    std::condition_variable tcvar;
};


#endif // __NOTIFY_H__